import Vuex from 'vuex'
import Router from "@/router/index.js";
import {login, logout} from "@/api/Login.js";


export default new Vuex.Store({
    state: {
        tabs: [
            {
                name: '首页',
                path: '/index'
            }
        ],
        user:{},
        loaded: false
    },
    mutations: {
        setTabs(state, tab){
            state.tabs.push(tab)
        },
        setUser(state,user){
            state.user = user
        },
        setLoaded(state,loaded){
            state.loaded = loaded
        }
    },
    actions: {
        Login({commit, dispatch}, user) {
            console.log(user)
            login(user).then(res => {
                console.log(res)
                if (res.code == 200) {
                    sessionStorage.setItem("satoken", res.data.tokenValue)
                    commit("setUser", res.data)
                    Router.push({path: "/index"})
                }
            }).catch(err => {
                console.log(err)
            })
        },
        logout({commit, dispatch}){
            logout().then(res=>{
                sessionStorage.removeItem("satoken")
                commit('setUser',{})
                Router.push({path: "/login"})
             }).catch(err=>{
                 console.log(err)
             })
        }
    }
})
